<?php 
/*
 * @分销商城
 * @功能说明：支付入口
 * @更新说明：暂无更新
 * @文件名 Ping.class.php
 * @编码 UTF-8
 * @创建时间 2016-4-11 下午1:50:56
 * @创建人 XING
 */
namespace service\pay;
require SCRIPT_ROOT.'/../../library/pay/ApiConstants.php';
require SCRIPT_ROOT.'/../../library/pay/DigestUtil.php';
class Pay {
    /*
	 * 分销商城_参数初始化
	 * @date: 2016-4-11 下午1:57:18
	 * @editor: XING
	 */
	const CHARSET = "UTF-8";
    const OPENAPI_ADDRESS = 'http://openapi.yijifu.net/gateway.html';
    const PARTNER_ID = '20140411020055684571';
    const SECURITY_KEY = 'c9cef22553af973d4b04a012f9cb8ea8';
    const HTTP_POST_PROTOCOL = 'httpPost';
    const SIGN_TYPE = 'MD5';
    const RETURN_URL = 'http://lywap.mfou.cn/pay/payCallback/jumpId/1';
    const NOTIFY_URL = 'http://lywap.mfou.cn/pay/payCallback';

    /*
	 * 分销商城_支付调起
	 * @date: 2016-4-11 下午1:56:40
	 * @editor: XING
	 */
    public static function main($order_id,$money){
        $params = self::initParams($order_id,$money);
        $responseMessage = self::exchange($params);
        echo $responseMessage;
        //echo "\r\n";
        //将JSON 格式的字符串并且把它转换为array map 变量
        $resultDataMap = json_decode($responseMessage, TRUE);
        //后续业务操作
        $resultCode = $resultDataMap['resultCode'];
        echo $resultCode;
    }

    /*
	 * 分销商城_参数初始化
	 * @date: 2016-4-11 下午1:55:15
	 * @editor: XING
	 */
    public static function initParams($order_id,$money){
        $data = array();
        // 基本参数
        $data[\ApiConstants::ORDER_NO] = $order_id;
        $data[\ApiConstants::PARTNER_ID] = self::PARTNER_ID;
        $data[\ApiConstants::PROTOCOL] = self::HTTP_POST_PROTOCOL;
        $data[\ApiConstants::SIGN_TYPE] = self::SIGN_TYPE;
        $data[\ApiConstants::NOTIFY_URL] = self::NOTIFY_URL;
        $data[\ApiConstants::RETURN_URL] = self::RETURN_URL;
        
        // 业务参数
        $data[\ApiConstants::SERVICE] = "qftIntegratedPayment";
        $data['outOrderNo'] = date('YmdHis').rand(11,99);
        $data['tradeChannel'] = 'CASHIER_MOBILE';
        $data['money'] = $money;
        $data['moneyReal'] = $money;
        $data['outPayeeShopId'] = '20140411020055684571';
        $data["payeeUserId"] = "20140411020055684571";
        $data["goodList"] = '['.json_encode(array('title'=>'商城订单支付')).']';
        
        return $data;
    }

    /*
	 * 分销商城_请求参数回调
	 * @date: 2016-4-11 下午1:56:18
	 * @editor: XING
	 */
    public static function exchange($param){
        $signStr = \DigestUtil::digest($param, self::SECURITY_KEY, self::SIGN_TYPE);
        
        $param = array_merge($param,array('sign'=>$signStr));
        $html='<html><head><meta http-equiv="Content-Type" content="text\html; charset=UTF-8" /></head><body onLoad="document.dinpayForm.submit();"><form name="dinpayForm" id="dinpayForm" method="POST" action="'.self::OPENAPI_ADDRESS.'" >';
        foreach($param as $k => $v){
        	$html .='<input type="hidden" name="'.$k.'" value="'.htmlentities($v,ENT_QUOTES,"UTF-8").'"/>';
        }
        $html .='</form>跳转中..</body>';
        echo $html;exit;
    }

    /*
	 * 分销商城_参数组装
	 * @date: 2016-4-11 下午1:58:00
	 * @editor: XING
	 */
    public static function buildPairs(array $param, $sign){
        $postStr = "";
        foreach ($param as $key => $value) {
            $postStr .= $key . '=' . urlencode(mb_convert_encoding($value, "utf-8", "auto")) . '&';
        }
        $postStr .= 'sign='.$sign;
        return $postStr;
    }

    /*
	 * 分销商城_检验参数合法性
	 * @date: 2016-4-11 下午4:41:58
	 * @editor: XING
	 */
    public function isLegel($params){
    	$signStr = \DigestUtil::digest($params, self::SECURITY_KEY, self::SIGN_TYPE);
    	return $signStr;
    }
}

?>